package com.okhttp.comment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;


import com.okhttp.R;

import java.util.List;

/**
 * @author 关忆北丶
 * @description:
 * @date :2021/3/25 14:57
 */
public class BaseCommentView extends BusinessLayout implements BusinessLayout.OnEndingViewListener {

    private RecyclerView mRvBaseComment;
    private Context mContext;
    private BaseCommentAdapter mBaseCommentAdapter;

    private IPCommentListener mIpCommentListener;
    private RelativeLayout mLlBaseCommentEmpty;
    private ImageView mIvBaseCommentEmptyIcon;
    private TextView mTvCommentEnd;

    private boolean isEmpty = false;

    public BaseCommentView(Context context) {
        super(context);
        init(context);
    }

    public BaseCommentView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public BaseCommentView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }


    public IPCommentListener getIpCommentListener() {
        return mIpCommentListener;
    }

    /**
     * 设置 默认显示得评论内容
     *
     * @param content
     */
    public void setContent(String content) {
        if (mIpCommentListener != null) {
            mIpCommentListener.setContent(content);
        }
    }


    private void init(Context context) {
        mContext = context;
        View mBaseCommentView = LayoutInflater.from(context).inflate(R.layout.view_base_comment, this);
        mTvCommentEnd = findViewById(R.id.tv_comment_end);
        mRvBaseComment = findViewById(R.id.rv_base_comment);
        mLlBaseCommentEmpty = findViewById(R.id.ll_base_comment_empty);
        mIvBaseCommentEmptyIcon = findViewById(R.id.iv_basecomment_empty_icon);

    }

    public void setEmptyViewListener(View.OnClickListener listener) {
        mLlBaseCommentEmpty.setOnClickListener(listener);
    }


    public void setAdapter(Activity mAct, IPCommentListener listener) {
        super.setActivity(mAct);
        if (listener == null) {
            mIpCommentListener = new IPCommentListener(mAct, this);
            mBaseCommentAdapter = new BaseCommentAdapter(mAct, mIpCommentListener);
        } else {
            mIpCommentListener = listener;
            mBaseCommentAdapter = new BaseCommentAdapter(mAct, listener);
        }
        mIpCommentListener.setCommentAdapterType(mBaseCommentAdapter);
        mRvBaseComment.setAdapter(mBaseCommentAdapter);
        super.setEndingViewListener(this);
    }

    /**
     * @param b = true 代表有数据 false代表没数据展示缺省
     */
    public void isEmptyView(boolean b) {
        this.isEmpty = b;
        if (b) {
            mRvBaseComment.setVisibility(VISIBLE);
            mLlBaseCommentEmpty.setVisibility(GONE);
        } else {
            mRvBaseComment.setVisibility(GONE);
            mLlBaseCommentEmpty.setVisibility(VISIBLE);
        }
    }

    //true 代表有数据 false代表没数据展示缺省
    public boolean isEmpty() {
        return isEmpty;
    }


    public BaseCommentAdapter getCommentAdapterType() {
        return mBaseCommentAdapter;
    }

    public void removeData(CommentBean.DataBean.CommentInfoBean norMarListBean) {
        if (mBaseCommentAdapter != null) {
            mBaseCommentAdapter.removeData(norMarListBean);
        }
    }

    public List<CommentBean.DataBean.CommentInfoBean> getData() {
        if (mBaseCommentAdapter != null) {
            return mBaseCommentAdapter.getData();
        }
        return null;
    }

    public void removeData(int pos) {
        if (mBaseCommentAdapter != null) {
            mBaseCommentAdapter.removeData(pos);
        }
    }


    public void setCommentBean(CommentBean.DataBean mCommentBean) {
        mBaseCommentAdapter.setCommentBean(mCommentBean);
    }


    public void setNewData(List<CommentBean.DataBean.CommentInfoBean> mNorMarListBeans) {
        if (mNorMarListBeans != null) {
            if (mBaseCommentAdapter != null) {
                mBaseCommentAdapter.setNorMarListBeans(mNorMarListBeans);
            }
        }
    }

    public void addData(List<CommentBean.DataBean.CommentInfoBean> mNorMarListBeans) {
        if (mNorMarListBeans != null && mNorMarListBeans.size() > 0) {
            if (mBaseCommentAdapter != null) {
                mBaseCommentAdapter.addNorMarListBeans(mNorMarListBeans);
            }

        }
    }


    /**
     * 设置缺省页图片大小
     *
     * @param height
     * @param width
     */
    public void setEmptyImgSize(int height, int width) {
        RelativeLayout.LayoutParams mLayoutParams = (RelativeLayout.LayoutParams) mIvBaseCommentEmptyIcon.getLayoutParams();
        mLayoutParams.height = height;
        mLayoutParams.width = width;
        mIvBaseCommentEmptyIcon.setLayoutParams(mLayoutParams);
    }

    /**
     * 设置缺省页距离上面
     *
     * @param topMargin
     */
    public void setEmptyTopMargin(int topMargin) {
        RelativeLayout.LayoutParams mLayoutParams = (RelativeLayout.LayoutParams) mIvBaseCommentEmptyIcon.getLayoutParams();
        mLayoutParams.topMargin = topMargin;
        mIvBaseCommentEmptyIcon.setLayoutParams(mLayoutParams);
    }


    @Override
    public void onEndingListener() {
        mTvCommentEnd.setVisibility(VISIBLE);
    }

    /**
     * 网络请求回来的具体 逻辑处理
     *
     * @param mPageNum 分页必须从 1 开始
     */
    public void commentNetData(CommentBean.DataBean response, int mPageNum) {
        if (response != null) {
            List<CommentBean.DataBean.CommentInfoBean> normalList = response.getNormalList();
            this.setCommentBean(response);
            if (mPageNum == 1) {
                this.isEmptyView(normalList.size() > 0);
                this.setNewData(normalList);
            } else {
                if (normalList.size() > 0) {
                    this.addData(normalList);

                }
            }
            if (normalList.size() > 0 && normalList.size() < CommentConstant.COMMENT_LOAD_COUNT) {
                //说明没数据了
                if (mOnEmptyViewListener != null) mOnEmptyViewListener.onEndingListener();
            }
        } else {
            this.isEmptyView(false);
        }
    }

    /**
     * 更新底部文案
     *
     * @param mContent
     */
    public void upSay(String mContent, LinearLayout llEliminate, ImageView ivAddCommentIcon, TextView tvAddComment) {
        if (TextUtils.isEmpty(mContent)) {

            llEliminate.setVisibility(View.GONE);
            if (ivAddCommentIcon != null) {
                ivAddCommentIcon.setVisibility(View.VISIBLE);
            }
            tvAddComment.setText("说点什么吧~");
            tvAddComment.setTextColor(getResources().getColor(R.color.color_999999));
        } else {
            llEliminate.setVisibility(View.VISIBLE);
            if (ivAddCommentIcon != null) {
                ivAddCommentIcon.setVisibility(View.GONE);
            }
            tvAddComment.setText(mContent);
            tvAddComment.setTextColor(getResources().getColor(R.color.color_333333));
            this.setContent(mContent);
            llEliminate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setContent("");
                    upSay("", llEliminate, ivAddCommentIcon, tvAddComment);
                }
            });
        }
    }


    /**
     * 评论详情 或者 弹出评论面板 数据发生变化 回执
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    public void commentOnResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == CommentConstant.COMMENT_REQUESTCODE) {
            if (data != null) {
                int mCommentType = data.getIntExtra("commentType", 0);
                boolean isViolation = data.getBooleanExtra("isViolation", false);
                if (resultCode == CommentConstant.COMMENT_RESPONSE) {
                    if (isViolation) {
                        //有违规内容
                        //mCommentHelp.showCommentViolation(activity);
                        return;
                    }
                    CommentReplyBean mCommentReplyBean = (CommentReplyBean) data.getSerializableExtra("replyBean");
                    if (mCommentReplyBean == null)
                        return;
                    boolean isShowReply = data.getBooleanExtra("isShowReply", false);// false = 一级评论

                    if (isShowReply) {
                        //产生二级评论
                        CommentReplyBean.DataBean newChildrenCommentData = mCommentReplyBean.getData();
                        IPCommentListener mIpCommentListener = this.getIpCommentListener();
                        if (mIpCommentListener != null) {
                            this.getCommentAdapterType().addNewChildrenCommentBeans(BusinessLayout.createReplyCommentsBean(newChildrenCommentData), mCommentType, mIpCommentListener.getPos());
                        }
                    } else {
                        if (!this.isEmpty()) {
                            this.isEmptyView(true);
                        }
                        //产生一级评论
                        this.getCommentAdapterType().addNewCommentBeans(mCommentReplyBean.getData());
                    }
                } else if (resultCode == CommentConstant.COMMENT_DETAILS) {
                    CommentBean.DataBean.CommentInfoBean mNorMarListBean = (CommentBean.DataBean.CommentInfoBean) data.getSerializableExtra("dataBean");
                    int mPosition = data.getIntExtra("position", 0);
                    if (!mNorMarListBean.isDelete()) {
                        this.getCommentAdapterType().replaceData(mPosition, mNorMarListBean);
                    } else {
                        this.getCommentAdapterType().removeData(mPosition);
                    }
                }
            }
        }
    }


    /**
     * 添加一个一级评论
     */
    public void commentOnResult(CommentReplyBean mCommentReplyBean) {
        if (!this.isEmpty()) {
            this.isEmptyView(true);
        }
        //产生一级评论
        this.getCommentAdapterType().addNewCommentBeans(mCommentReplyBean.getData());
    }


}
